package com.hongye.why.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;

/**
 * SharePreference 缓存工具
 */
public class PreferenceUtil {
    /**
     * 将对象以 JSON 格式保存本地
     * @param key
     * @param obj
     */
    public static void saveObj(Context context, String key, Object obj) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        String jsonStr = JSON.toJSONString(obj);
        settings.edit().putString(key, jsonStr).apply();
    }

    /**
     * 将本地 JSON 以对象的形式读出
     * @param context
     * @param key String 对应键值
     * @param tClass
     * @param <T> 读出后转换的对象
     * @return
     */
    public static <T> T getObj(Context context, String key, Class<T> tClass) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        String result = settings.getString(key, "");
        if (!TextUtils.isEmpty(result)) {
           try {
               return JSON.parseObject(result, tClass);
           } catch (Exception e) {
               e.printStackTrace();
           }

        }
        return null;
    }

    /**
     * 读取本地对应的字符串
     * @param context
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getPrefString(Context context, String key,
                                       final String defaultValue) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        return settings.getString(key, defaultValue);
    }

    /**
     * 将字符串保存到本地
     * @param context
     * @param key
     * @param value
     */
    public static void setPrefString(Context context, final String key,
                                     final String value) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        settings.edit().putString(key, value).apply();
    }

    public static boolean getPrefBoolean(Context context, final String key,
                                         final boolean defaultValue) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        return settings.getBoolean(key, defaultValue);
    }

    /**
     * 本地是否含有key对应缓存资源
     * @param context
     * @param key
     * @return
     */
    public static boolean hasKey(Context context, final String key) {
        return PreferenceManager.getDefaultSharedPreferences(context).contains(
                key);
    }

    public static void setPrefBoolean(Context context, final String key,
                                      final boolean value) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        settings.edit().putBoolean(key, value).apply();
    }

    public static void setPrefInt(Context context, final String key,
                                  final int value) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        settings.edit().putInt(key, value).apply();
    }

    public static int getPrefInt(Context context, final String key,
                                 final int defaultValue) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        return settings.getInt(key, defaultValue);
    }

    public static void setPrefFloat(Context context, final String key,
                                    final float value) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        settings.edit().putFloat(key, value).apply();
    }

    public static float getPrefFloat(Context context, final String key,
                                     final float defaultValue) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        return settings.getFloat(key, defaultValue);
    }

    public static void setSettingLong(Context context, final String key,
                                      final long value) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        settings.edit().putLong(key, value).apply();
    }

    public static long getPrefLong(Context context, final String key,
                                   final long defaultValue) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        return settings.getLong(key, defaultValue);
    }

    /**
     * 清空本地SharePreference缓存
     * @param context
     * @param p
     */
    public static void clearPreference(Context context,
                                       final SharedPreferences p) {
        final SharedPreferences.Editor editor = p.edit();
        editor.clear();
        editor.apply();
    }

    /**
     * 删除本地key对应的缓存
     * @param context
     * @param key
     */
    public static void removeSpKey(Context context,
                                   String key) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        settings.edit().remove(key).apply();
    }
}
